﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DACU.Player;
using DACU.Tools;

namespace DACU.UserControls.Player
{
	public partial class VolumePopup
	{
		public event ChangeVolumeEventHandler VolumeChanged;

		public float Volume
		{
			get
			{
				float f = 0f;
				sVolume.Dispatcher.Invoke(new Action(() => { f = (tbMute.IsChecked ?? false) ? 0 : (float) sVolume.Value; }));
				return f;
			}
			set
			{
				sVolume.Dispatcher.Invoke(new Action(() =>
				                                     	{
															if (!(tbMute.IsChecked ?? false))
																sVolume.Value = value;
				                                     	}));
			}
		}

		public Window OwnerWindow { get; set; }

		public VolumePopup()
		{
			InitializeComponent();
		}

		public VolumePopup(Window owner)
		{
			OwnerWindow = owner;
			InitializeComponent();
		}

		private void sVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			if (VolumeChanged != null)
				VolumeChanged(Volume);
			tbMute.IsChecked = sVolume.Value < 1;
			if (tbMute.IsChecked ?? false)
				tbMute_Click(tbMute, null);
		}

		private void tbMute_Click(object sender, RoutedEventArgs e)
		{
			if (tbMute.IsChecked ?? false)
			{
				if (VolumeChanged != null)
					VolumeChanged(0);
			}
			else
			{
				if (VolumeChanged != null)
					VolumeChanged((float)sVolume.Value);
			}
			Keyboard.ClearFocus();
		}
	}
}
